/*********************************************************************************************************
*  Color Management
*
*  manages a fixed-set of colors. Rule-based color management
*
*  Author of the code: Florian Weil - www.derhess.de
*
************************************************************************************************************/

inlets = 1;
outlets = 3;



// Create color themes inspired by http://kuler.adobe.com/
var burmaSet = new ColorSet(new RGBValue(53,66,66),
							new RGBValue(172,235,174),
							new RGBValue(255,255,157),
							new RGBValue(201,222,85),
							new RGBValue(125,145,0),
							"random");
							
var burmaPassionSet = new ColorSet(new RGBValue(161,23,45),
							new RGBValue(253,255,186),
							new RGBValue(167,219,158),
							new RGBValue(39,92,87),
							new RGBValue(31,27,25),
							"random");
							
var autumnSet = new ColorSet(new RGBValue(64,133,105),
							new RGBValue(160,171,56),
							new RGBValue(235,187,37),
							new RGBValue(255,144,36),
							new RGBValue(255,95,52),
							"random");
							
var warmSet = new ColorSet(new RGBValue(22,4,25),
							new RGBValue(89,0,30),
							new RGBValue(194,40,3),
							new RGBValue(255,118,18),
							new RGBValue(255,231,12),
							"contrast");
							
var coldSet = new ColorSet(new RGBValue(48,37,54),
							new RGBValue(52,87,104),
							new RGBValue(57,137,152),
							new RGBValue(61,186,202),
							new RGBValue(65,236,250),
							"contrast");
						
var oldJacketSet = new ColorSet(new RGBValue(44,59,37),
							new RGBValue(182,169,117),
							new RGBValue(230,197,152),
							new RGBValue(213,145,109),
							new RGBValue(166,50,50),
							"contrast");
							
var dirtSet = new ColorSet(new RGBValue(23,15,14),
							new RGBValue(41,4,24),
							new RGBValue(80,82,23),
							new RGBValue(255,211,114),
							new RGBValue(255,241,175),
							"random");

var colorThemeList = new Array(burmaSet,burmaPassionSet,autumnSet,warmSet,coldSet,oldJacketSet,dirtSet);
var currentColorSet = burmaSet;
var counterTheme = 0;

/*********************************************************************************************************
* Max MSP API Functions
************************************************************************************************************/
function readNewColor()
{
	var tRGBvalue = currentColorSet.getColor();
	outlet(0, tRGBvalue.getRed());
    outlet(1, tRGBvalue.getGreen());
	outlet(2, tRGBvalue.getBlue());
}

function changeTheme()
{
	if(counterTheme >= colorThemeList.length)
	{
		counterTheme = 0;
	}
	
	currentColorSet = colorThemeList[counterTheme];
	readNewColor();
	counterTheme++;
}

/*********************************************************************************************************
* Classes for the Color Management
************************************************************************************************************/
///////// Color Value Class ////////////////
function RGBValue(redValue,greenValue,blueValue)
{
	this.red = redValue;
	this.green = greenValue;
	this.blue = blueValue;
}

function getRed()
{
	return this.red;
}
RGBValue.prototype.getRed = getRed;

function getGreen()
{ 
	return this.green;
}
RGBValue.prototype.getGreen = getGreen;

function getBlue()
{
	return this.blue;
}
RGBValue.prototype.getBlue = getBlue;

///////// ColorSet Class ////////////////
function ColorSet(col1,col2,col3,col4,col5,set_type)
{
	this.type = set_type; // "random" or "contrast"
	this.color_set = new Array(col1,col2,col3,col4,col5);
	this.currentColor = col1;
	this.currentColorIndex = 0;
}

function getColor()
{
	if(this.type == "random")
		return this.randomColor();
	else if(this.type == "contrast")
		return this.contrastColor();
}
ColorSet.prototype.getColor = getColor;

function contrastColor()
{
	this.currentColorIndex += 1;
	
	if(this.currentColorIndex >= this.color_set.length)
		this.currentColorIndex = 0;
		
	this.currentColor = this.color_set[this.currentColorIndex];
	return this.currentColor;
	
}
ColorSet.prototype.contrastColor = contrastColor;

function randomColor()
{
	var tColorIndex = getRandom(0,4);
	if(tColorIndex == this.currentColorIndex) {
		return this.randomColor();
	} else {
		this.currentColorIndex = tColorIndex;
		this.currentColor = this.color_set[this.currentColorIndex];
		return this.currentColor;
	}
}
ColorSet.prototype.randomColor = randomColor;
//////////////////////////////////////////////////////////////////////////
//    Helper Functions
//////////////////////////////////////////////////////////////////////////
function getRandom( min, max ) {
	if( min > max ) {
		return( -1 );
	}
	if( min == max ) {
		return( min );
	}
 
    return( min + parseInt( Math.random() * ( max-min+1 ) ) );
}
